﻿using System;
using System.Collections.Generic;
using System.Linq;
using Luna.Data.Budget;
using Luna.Model.Budget;
using Rhino.Mocks;

namespace Luna.Services.Test.Budget
{
    public class CategoryTest : EntityTest<IBudgetProvider, IEditableCategory, Guid, ICategoryData>
    {
        public override void SetUp()
        {
            base.SetUp();
            ApplicationModel.Budget.Reset();
            PropertyShifters.Add(new PropertyShifter<IEditableCategory, string>(x => x.Name, DataManager.GetString));
            PropertyShifters.Add(new PropertyShifter<IEditableCategory, string>(x => x.Description, DataManager.GetString));
        }

        protected override ICategoryData CreateMockData()
        {
            return BudgetDataManager.CreateCategoryData();
        }

        protected override Func<IBudgetProvider, IEnumerable<ICategoryData>> DataGetAll
        {
            get { return p => p.CategoryGetAll(); }
        }

        protected override Func<IBudgetProvider, Guid, ICategoryData> DataGet
        {
            get { return (p, k) => p.CategoryGet(k); }
        }

        protected override Action<IEditableCategory> EntityDelete
        {
            get { return ApplicationModel.Budget.Delete; }
        }

        protected override Action<IBudgetProvider, Guid> DataDelete
        {
            get { return (p, k) => p.CategoryDelete(k); }
        }

        protected override Action<IEditableCategory> EntitySave
        {
            get { return ApplicationModel.Budget.Save; }
        }

        protected override Action<IBudgetProvider, ICategoryData> DataSave
        {
            get { return (p, i) => p.CategorySave(i); }
        }

        protected override IBudgetProvider DataProvider
        {
            get { return DataContext.Budget; }
        }

        protected override IEnumerable<IEditableCategory> EntitiesAll
        {
            get { return ApplicationModel.Budget.Categories.Select(x => x.EditableCopy); }
        }

        protected override IEditableCategory EntityGet(Guid P_Key)
        {
            return ApplicationModel.Budget.Categories[P_Key].EditableCopy;
        }

        protected override ICategoryData SaveMatch(IEditableCategory P_Entity)
        {
            return Arg<ICategoryData>.Matches(
                 a => DataKeyMatch(a.PK, P_Entity)
                     && a.Name == P_Entity.Name
                     && a.Description == P_Entity.Description);
        }

        protected override bool BuildMatch(IEditableCategory P_Entity, ICategoryData P_Data)
        {
            return DataKeyMatch(P_Data.PK, P_Entity) && P_Entity.Name == P_Data.Name && P_Entity.Description == P_Data.Description;
        }

        protected override Guid DataKey(ICategoryData P_Data)
        {
            return P_Data.PK;
        }

        protected override bool DataKeyMatch(Guid P_Parameter, IEditableCategory P_Match)
        {
            return P_Parameter == P_Match.ID;
        }
    }
}